﻿using DetectingKinectSencor._40_Tools;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard
{
    class WordsPerMinuteViewModelDashboard : Notifier
    {
        private DateTime lastElementCaptureTime;
        private TimeSpan interval = new TimeSpan(0,0,60);
        private TimeSpan actualInterval = new TimeSpan(0, 0, 0);

        public WordsPerMinuteViewModelDashboard(DateTime lastElementCaptureTime)
        {
            this.lastElementCaptureTime = lastElementCaptureTime;
            tempPlotModel = new PlotModel();
            SetUpModel();
            LoadData(lastElementCaptureTime);
        }

        // Internal variable in which the model is saved in this class
        private PlotModel plotModel;
        public PlotModel tempPlotModel;
        private LineSeries timepointSerieWordsPerMinuteList;
        private TimeSpan actualIntervalUpperBound;
        private int totalAmountWordsPerMinute;

        // Important the UI needs to bind the Model to SlideChangePlotModel!!!
        public PlotModel WordsPerMinutePlotModel
        {
            get { return plotModel; }
            set { plotModel = value; RaisePropertyChanged(() => WordsPerMinutePlotModel); }
        }

        private void SetUpModel()
        {
            tempPlotModel.LegendTitle = "Legend";
            tempPlotModel.LegendOrientation = LegendOrientation.Horizontal;
            tempPlotModel.LegendPlacement = LegendPlacement.Outside;
            tempPlotModel.LegendPosition = LegendPosition.TopRight;
            tempPlotModel.LegendBackground = OxyColor.FromAColor(200, OxyColors.White);
            tempPlotModel.LegendBorder = OxyColors.Black;
            tempPlotModel.LegendFontSize = DesignOfGraphs.fontSizeDashboard;


            // Axes which shows the date when the picture was shown
            TimeSpanAxis timeLineAxis = new TimeSpanAxis(AxisPosition.Bottom, "Timeline (mm:ss)", "mm:ss")
                                                        {
                                                            MajorGridlineStyle = LineStyle.Solid,
                                                            MinorGridlineStyle = LineStyle.Dot,
                                                            FontSize = DesignOfGraphs.fontSizeDashboard
                                                        };

            // Add the axes to the plot model
            tempPlotModel.Axes.Add(timeLineAxis);

            LinearAxis valueAxis = new LinearAxis(AxisPosition.Left,"Number of words per minute")
                                                 {
                                                     MajorGridlineStyle = LineStyle.Solid,
                                                     MinorGridlineStyle = LineStyle.Dot,
                                                     FontSize = DesignOfGraphs.fontSizeDashboard
                                                 };
            tempPlotModel.Axes.Add(valueAxis);

        }

        public void LoadData(DateTime captureTime)
        {
            actualInterval = new TimeSpan(0, 0, 0);
            actualIntervalUpperBound = new TimeSpan(0, 0, 0);

            // Get the list with the viewingDirectionData
            WordsPerMinuteData oralCommunicationList = EvaluationData.oralCommunication;
            TimeSpan elapsedTime = new TimeSpan();

            timepointSerieWordsPerMinuteList = new StairStepSeries
            {              
                CanTrackerInterpolatePoints = false,
                VerticalLineStyle = LineStyle.Dash,
                Title = string.Format("Words per minute"),
                Smooth = false,
                Color = OxyColors.SlateGray
            };

            timepointSerieWordsPerMinuteList.Points.Add(new DataPoint(0,0));


            totalAmountWordsPerMinute = 0;

            foreach (CaptureTime oralCommunication in oralCommunicationList.trackingSequences)
            {
                if (captureTime == oralCommunication.captureTime)
                {
                    for (int i = 0; i < oralCommunication.timePoints.Count; i++)
                    {

                        totalAmountWordsPerMinute += oralCommunication.timePoints[i].wordsPerMinute;

                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = oralCommunication.timePoints[i].timePoint - captureTime;

                        actualIntervalUpperBound += interval;

                        if ((elapsedTime >= actualInterval) && (elapsedTime <= actualIntervalUpperBound))
                        {

                            timepointSerieWordsPerMinuteList.Points.Add(new DataPoint
                                                                        (TimeSpanAxis.ToDouble(actualInterval),
                                                                        oralCommunication.timePoints[i].wordsPerMinute));
                        }
                        else
                        {
                            timepointSerieWordsPerMinuteList.Points.Add(new DataPoint
                                                                        (TimeSpanAxis.ToDouble(actualInterval),
                                                                        0));
                            //Decrement the indexer, to use the same timePoint and check for the next interval
                            i--;
                        }

                        // Increase the actual interval
                        actualInterval += interval;

                    }

                    timepointSerieWordsPerMinuteList.Points.Add(new DataPoint(TimeSpanAxis.ToDouble(actualIntervalUpperBound), 0));

                    tempPlotModel.Series.Add(timepointSerieWordsPerMinuteList);
                    break;
                }
            }

            Console.WriteLine("totalAmountWordsPerMinute: " + totalAmountWordsPerMinute);
            

            //Saved the tempPlotModel to the plotModel than is the OnPropertyChanged executed and the UI is updated
            WordsPerMinutePlotModel = tempPlotModel;
        }
    }
}
